package com.infore.common.utils;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FileUtil {

	/**
	 * 
	 * @Description: 返回文件类型 根据后缀
	 * @param fileName
	 * @return
	 * @date 2018年1月11日
	 * @author cys
	 */
	public static String fileType(String fileName) {
		String cls = null;
		if (!StringUtils.isBlank(fileName)) {
			String ftype = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
			ftype = ftype.toLowerCase();
			if (ftype.equals("doc") || ftype.equals("docx")) {
				cls = "word";
			} else if (ftype.equals("xls") || ftype.equals("xlsx")) {
				cls = "excel";
			} else if (ftype.equals("ppt") || ftype.equals("pptx")) {
				cls = "ppt";
			} else if (ftype.equals("pdf")) {
				cls = "pdf";
			} else if (ftype.equals("png") || ftype.equals("jpg")) {
				cls = "image";
			} else if (ftype.equals("dwg")) {
				cls = "cad";
			} else if (ftype.equals("mpp")) {
				cls = "project";
			} else if (ftype.equals("txt")) {
				cls = "txt";
			} else {
				cls = ftype;
			}

		}

		return cls;
	}

	public static String fileNameSuffix(String fileName) {
		String ftype = "";
		if (!StringUtils.isBlank(fileName)) {
			ftype = fileName.substring(fileName.lastIndexOf("."), fileName.length());
			ftype = ftype.toLowerCase();
		}

		return ftype;
	}

	/**
	 * 文件下载
	 * 
	 * @author 蔡永顺
	 * @return void
	 * @param response
	 * @param furl     文件地址
	 * @param filename 文件名
	 * @throws IOException
	 * @date 2019年9月30日上午9:43:06
	 */
	public void downloadFileRes(HttpServletResponse response, String furl, String filename) throws IOException {
		String error = "";
		try {
			byte[] bytes = null;
			File file = new File(furl);
			// File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			bytes = bos.toByteArray();
			if (bytes != null) {
//				filename = java.net.URLEncoder.encode(filename, "UTF-8");
				// 清空response
				response.reset();
				// 设置response的Header
				response.setContentType("application/octet-stream;charset=utf-8");
				response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
				response.addHeader("Content-Length", "" + bytes.length);
				OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
				toClient.write(bytes);
				toClient.flush();
				toClient.close();
			}
		} catch (FileNotFoundException e) {
			// e.printStackTrace();
			// filename = java.net.URLEncoder.encode(filename, "UTF-8");
			error = "文件不存在!!";
			response.setContentType("text/plain;charset=utf-8");
			response.getWriter().write(error);
		} catch (Exception e) {
			e.printStackTrace();
			// filename = java.net.URLEncoder.encode(filename, "UTF-8");
			response.setContentType("text/plain;charset=utf-8");
			response.getWriter().write(error);
		}
	}

	/**
	 * 
	 * @Description: 删除文件 及其子文件
	 * @param file
	 * @return
	 * @date 2018年2月1日
	 * @author cys
	 */
	public static boolean deleteFile(File file) {
		boolean success = false;
		if (file != null && file.exists()) {
			if (file.isFile()) { // 判断是否是文件
				success = file.delete(); // 删除文件
			} else if (file.isDirectory()) { // 否则如果它是一个目录
				File[] files = file.listFiles(); // 声明目录下所有的文件 files[];
				if (files != null) {
					for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
						deleteFile(files[i]); // 把每个文件用这个方法进行迭代
					}
				}

				success = file.delete(); // 删除文件夹
			}
		}
		return success;
	}

	/**
	 * 
	 * @Description: 删除文件夹下的所有文件 不包含自身
	 * @param delpath
	 * @return
	 * @throws Exception
	 * @date 2018年2月11日
	 * @author cys
	 */
	public static boolean deletefile(String delpath) throws Exception {
		boolean success = false;
		if (StringUtils.isBlank(delpath)) {
			return false;
		}
		try {

			File file = new File(delpath);
			// 当且仅当此抽象路径名表示的文件存在且 是一个目录时，返回 true
			if (!file.isDirectory()) {
				success = file.delete();
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				if (filelist != null) {
					for (int i = 0; i < filelist.length; i++) {
						File delfile = new File(delpath + "\\" + filelist[i]);
						if (!delfile.isDirectory()) {
							delfile.delete();
						} else if (delfile.isDirectory()) {
							deletefile(delpath + "\\" + filelist[i]);
						}
					}
				}
				if (!file.toString().equals("G:\\kuoshandata\\")) { // 选择不删除自身文件夹
					file.delete();
				}
			}

		} catch (FileNotFoundException e) {
		}
		return success;
	}

}
